package com.asia5b.a5bmall.same.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import com.asia5b.a5bmall.same.R
import com.asia5b.a5bmall.same.adapter.SelectCateAdapter
import com.asia5b.a5mall.middleware.model.home.CategoryModel
import com.asia5b.a5mall.middleware.base.BaseDialog
import com.asia5b.a5mall.middleware.utils.newDivider
import kotlinx.android.synthetic.main.dialog_selectbase.*

/**
 * Created by zhangyuncai on 2020/5/27.
 */
class ApplyCateSelectDialog(context: Context) : BaseDialog(context) {
    override val layoutId: Int = R.layout.dialog_selectbase
    override val heightPercent: Double = 0.6

    var goods_management_category = ""
    var title: String? = null
    val mDatas = arrayListOf<CategoryModel>()
    val mAdapter by lazy {
        var allChecked = true
        val cateTexts = goods_management_category.split(",")
        for (categoryModel in mDatas) {
            categoryModel.checked = cateTexts.contains("${categoryModel.catId}")
            if (allChecked && !categoryModel.checked) {
                allChecked = false
            }
        }
        val categoryModel = CategoryModel()
        categoryModel.enName = getString(R.string.全选)
        categoryModel.checked = allChecked
        mDatas.add(0, categoryModel)
        SelectCateAdapter(mDatas)
    }
    val mRecyclerView by lazy {
        rv_selectbase
    }

    override fun initView() {
        title?.let {
            tv_selectbase_title.text = it
        }
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(context)!!
            adapter = mAdapter
            addItemDecoration(newDivider(context))
        }
    }

    override fun initEvent() {
        bt_selectbase_sure.setOnClickListener {
            val cateIds = arrayListOf<Int>()
            for (categoryModel in mDatas) {
                if (!TextUtils.equals(categoryModel.showName(), getString(R.string.全选)) && categoryModel.checked) {
                    cateIds.add(categoryModel.catId)
                }
            }
            val cateIdsText = TextUtils.join(",", cateIds)
            mSureClick?.invoke(cateIdsText)
            dismiss()
        }
        tv_selectbase_close.setOnClickListener {
            mCancelClick?.invoke()
            dismiss()
        }
    }


}